package com.ptbuy.tranrecord.product.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONObject;
import com.ptbuy.R;
import com.ptbuy.db.dao.ProductDao;
import com.ptbuy.db.pojo.ProductPojo;
import com.ptbuy.utils.Utils;

import java.text.DecimalFormat;
import java.util.List;

public class RecyclerProductViewAdapter extends RecyclerView.Adapter<RecyclerProductViewAdapter.RecyclerViewHolder> {


    ProductDao productDao;
    Context mContext;

    public interface ProjectInterface {
        void optProject(int type, ProductPojo dataBean, boolean isChecked);
    }

    public ProjectInterface projectInterface;

    public void setProjectInterface(ProjectInterface projectInterface) {
        this.projectInterface = projectInterface;
    }

    private void optProject(int type, ProductPojo dataBean, boolean isChecked) {
        projectInterface.optProject(type, dataBean, isChecked);
    }


    private List<ProductPojo> data;

    //有参构造器，给外部传数据进来适配器
    public RecyclerProductViewAdapter(List<ProductPojo> data, Context context) {
        this.data = data;
        this.mContext = context;
        if (productDao == null) {
            productDao = new ProductDao();
        }
    }

    //创建视图
    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.tran_product_card_layout, parent, false);
        return new RecyclerViewHolder(v);
    }

    //赋值操作
    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(RecyclerViewHolder holder, int position) {

        ProductPojo productPojo = data.get(position);
        holder.itemName.setText(productPojo.getProductName());//商品名称
        holder.itemNameProductOrder.setText((data.size() - position) + "");//商品名称
        String productPrice = productPojo.getProductPrice();

        String dealPrice = Utils.getNumlStr(productPrice);

        holder.itemPrice.setText(dealPrice + "元");//商品价格
        String time = productPojo.getTime();
        String s = Utils.stringToTime(time);
        holder.itemTime.setText(s);//记录时间

        //删除
        holder.itemRemove.setOnClickListener(v -> {
            optProject(2, productPojo, false);
        });
        holder.itemProductModify.setOnClickListener(v -> {
            optProject(4, productPojo, false);
        });

        String mult = productPojo.getMult();
        if (mult == null || "".equalsIgnoreCase(mult)) {
            holder.productDetailImg.setImageResource(R.drawable.no_image);
        } else {
            List<String> list = JSONObject.parseArray(mult, String.class);
            Bitmap bitmap = BitmapFactory.decodeFile(list.get(0));
            holder.productDetailImg.setImageBitmap(bitmap);
        }
        //查看照片视频
        holder.itemView.setOnClickListener(v -> {
            optProject(3, productPojo, false);
        });
    }

    //条目的数量
    @Override
    public int getItemCount() {
        return data.size();
    }

    //声明控件，绑定控件
    public class RecyclerViewHolder extends RecyclerView.ViewHolder {

        TextView itemName;//商品名称
        TextView itemPrice;//商品价格
        TextView itemTime;//记录时间
        TextView itemNameProductOrder;//编号
        ImageView productDetailImg;//编号
        //动作
        Button itemRemove;//删除
        Button itemView;//查看照片视频
        Button itemProductModify;//查看照片视频

        public RecyclerViewHolder(View v) {
            super(v);
            itemName = v.findViewById(R.id.itemName);
            itemNameProductOrder = v.findViewById(R.id.itemNameProductOrder);
            productDetailImg = v.findViewById(R.id.productDetailImg);
            itemPrice = v.findViewById(R.id.itemPrice);
            itemTime = v.findViewById(R.id.itemTime);
            itemRemove = v.findViewById(R.id.itemRemove);
            itemView = v.findViewById(R.id.itemView);
            itemProductModify = v.findViewById(R.id.itemProductModify);
        }
    }
}